package housing.task5.jaxb;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.File;

/**
 * Deserializes records from .xml file into List<Obj>
 * <p/>
 * Created by IntelliJ IDEA.
 * User: Linderhof
 * Date: 05.12.2010
 * Time: 1:01:33
 */
public class Deserializer {
//    public static IXmlCollectable deserialize(IXmlCollectable collection, String filePath) {
//        IXmlCollectable collection = new IXmlCollectable();
//        Renters renters = new Renters();
//
//        try {
//            JAXBContext jc = JAXBContext.newInstance(Renters.class);
//
//            Unmarshaller unmarshaller =
//                    null;
//            try {
//                unmarshaller = jc.createUnmarshaller();
//                renters.getRenter().addAll(
//                        ((Renters) unmarshaller.unmarshal(new File(filePath))).getRenter());
//            } catch (JAXBException e) {
//                System.out.println("Failed unmarshall!!!");
//
//                //e.printStackTrace();
//            }
//
//
//        } catch (JAXBException e) {
//            e.printStackTrace();
//        }
//
//        return renters;
//    }

    public static Renters deserialize(String filePath) {
        Renters renters = new Renters();

        try {
            JAXBContext jc = JAXBContext.newInstance(Renters.class);

            Unmarshaller unmarshaller =
                    null;
            try {
                unmarshaller = jc.createUnmarshaller();
                renters.getRenter().addAll(
                        ((Renters) unmarshaller.unmarshal(new File(filePath))).getRenter());
            } catch (JAXBException e) {
                System.out.println("Failed unmarshall!!!");

                //e.printStackTrace();
            }


        } catch (JAXBException e) {
            e.printStackTrace();
        }

        return renters;
    }

}//class Deserizalizer
